<template>
	<view>
		<view class="kc_title">
			<text>{{ name }}</text>
			
		</view>
		<tree @navigateToDetail="navigateToDetail" :treeListIndex="treeListIndex" :treeList="item" v-for="(item, index) in dir_list" :key="index"></tree>
	</view>
</template>

<script>
	export default{
		data() {
			return {
				name: "",
				
				dir_list: [],
				treeListIndex: 0,
				treeList: {
				      text: '第一章',
				      id: 0,
					  level: 1,
				      nodes: [
				        {
				          text: '第一节',
				          id: 1,
						  level: 2,
				          nodes: [
				            {
				              text: '1.__________',
				              id: 23,
				            },
							{
							  text: '2.__________',
							  id: 21,
							},
							{
							  text: '3.__________',
							  id: 22,
							},
				          ]
				        },
				        ]
				    }
			}
		},
		methods: {
			navigateToDetail(data) {
				uni.navigateTo({
					url:'./detail?chapterId='+data.id + "&chapterName=" + data.chapterName
				})
			},
			getDirById(chapterId) {
			  return new Promise((resolve, reject) => {
				this.pyReq(
				  this.$http.getChildChapter,
				  {
					chapterId,
				  },
				  (res) => {
					if(res) resolve(res);
				  }
				);
			  });
			},
		},
		onLoad(option) {
			this.name = option.name
			var that = this;
			that.pyReq(that.$http.getMindClazzDetailSecond, { mindClazzId: option.id },(res) => {
				if(res.code == 0) {
					const { 
						chapters
					} = res.result;
					that.chapters = chapters;
					// that.dir_list = chapters.map(item => {
					// 	if(item.isEnd == 0) {
							
					// 		return {
					// 			text: item.chapterName,
					// 			id: item.id,
					// 			level: 1,
					// 		}
					// 	} else {
					// 		return {
					// 			text: item.chapterName,
					// 			id: item.id,
					// 			level: 1,
					// 		}
					// 	}
					// });
					
					
					let promissArr = chapters.map(async item => {
					  if(item.isEnd == 0) {
						let data = await this.getDirById(item.id);
						return {
						  ...item,
						  text: item.chapterName,
						  level: 1,
						  nodes: data.result.map(ite => {
							return {
							  ...ite,
							  parendId: item.id,
							  text: ite.chapterName,
							}
						  }) || []
						};
					  } else {
						return {
						  ...item,
						  level: 1,
						  text: item.chapterName
						};
					  }
					});
					let proarr = [];
					let func_arr = [];
					Promise.all(promissArr).then(res => {
					  var topData = JSON.parse(JSON.stringify(res));
					  topData.forEach(item => {
						if(item.isEnd == 0) {
						  proarr.push(...item.nodes.filter(item => item.isEnd  == 0));
						} else {
						  proarr.push(item);
						}
					  });
		
					  func_arr = proarr.map(async item => {
						if(item.isEnd == 0) {
						  let data = await this.getDirById(item.id);
						  return {
							...item,
							text: item.chapterName,
							level: 2,
							nodes: data.result.map(item => {
							  return {
								...item,
								text: item.chapterName
							  }
							})
						  }
						} else {
						  return {
							...item,
							level: 2,
							text: item.chapterName
						  };
						}
					  });
					  Promise.all(func_arr).then(res => {
						that.dir_list = topData.map(item => {
						  if(item.isEnd == 0) {
							let arr = res.filter(ite => item.id == ite.parendId);
							if(arr.length) {
							  return {
								...item,
								nodes: arr
							  }
							} else {
							  return item;
							}
						  } else {
							return item
						  }
						});
					  });
					});	            
				}
			});
		},
	}
</script>

<style>
	.kc_title {
		font-size: 40rpx;
		font-weight: bolder;
		text-align: center;
		padding: 10rpx 0;
	}
</style>
